<?php
  require_once("db_connect.php");
  $btn_name = 'Add me';
  $table_title = "Add Country";
  //ALTER TABLE `countries` ADD `c_image` VARCHAR( 255 ) NOT NULL
  $id = isset($_GET['id'])?$_GET['id']:0;
  $updateCountryId = "";
  //pr($_REQUEST);
  
  
  
  ////Delete Country ////
  if(isset($_GET['mode']) && $_GET['mode']=='delete'){
    $delCountryQuery = "DELETE FROM `countries` WHERE `id`=$id";
    mysql_query($delCountryQuery)or die(mysql_error());
    $deletedRows = mysql_affected_rows();
    if($deletedRows == 1){
      echo "<h4>Country Deleted</h4>";
    }
  }
  ////Update Country ////
  if(isset($_GET['mode']) && $_GET['mode']=='edit'){
    $btn_name = 'UPDATE ME';
    $table_title = "UPDATE Country";
    $selCountryQuery = "SELECT * FROM `countries` WHERE `id`=$id";
    $resultCountryQuery = mysql_query($selCountryQuery)or die(mysql_error());
    $ansCountryQuery =  mysql_fetch_assoc($resultCountryQuery);
    $updateCountryId = $ansCountryQuery['id'];
    $updateCountryName = $ansCountryQuery['name'];
    $c_image = $ansCountryQuery['c_image'];
    //pr($ansCountryQuery);

  }

  // Update after post
  if(isset($_POST['name']) && !empty($_POST['id'])) {
    $postedId = $_POST['id'];
    $updatedName = $_POST['name'];
//print_r($_FILES);
//print_r($_POST);

############### UPDATE IMAGE START #################
    if($_FILES['c_image']['error']==0){
      $src = $_FILES['c_image']['tmp_name'];
      $filename = date('ymdHis').'_'.$_FILES['c_image']['name'];
      $desc = "c_images/$filename";
      move_uploaded_file($src, $desc);
      // unlink is used to remove old uploaded image
      unlink("c_images/".$_POST['hidden_c_image']);
    } else {
      $filename = $_POST['hidden_c_image'];
    }
############# END ########################

    $updateCountryQuery = "UPDATE `countries` SET `name`='$updatedName' ,  `c_image` ='$filename'  WHERE `id`='$postedId'";

    $resultCountryQuery = mysql_query($updateCountryQuery)or die(mysql_error());
    if(mysql_affected_rows() == 1){
      echo "<h4>Country Updated</h4>";
    }
    header('location:add_country.php');
    //$updateCountryName = '';
    //$btn_name = 'Add me';
    //unset($_GET['mode']);
    //$table_title = "Add Country";
    //pr($updateCountryQuery);
  }

  //// Save Country ////
  if(isset($_POST['name']) && empty($_POST['id'])) {

if (!empty($_REQUEST['captcha'])) {
    if (trim(strtolower($_REQUEST['captcha'])) != $_SESSION['secretword']) {
        echo "<strong style=\"background-color: #FF606C\">Invalid Captcha</strong>";
    } else {
      if($_FILES['c_image']['error']==0){
        $src = $_FILES['c_image']['tmp_name'];
        $filename = date('ymdHis').'_'.$_FILES['c_image']['name'];
        $desc = "c_images/$filename";
        move_uploaded_file($src, $desc);
      }

      //echo "Yes POsted";
      $name = $_POST['name'];
      $query = "INSERT INTO `countries`(`name`,`c_image`) VALUES('$name','$filename')";
      mysql_query($query) or die(mysql_error());
      $rowsAffected = mysql_affected_rows();
      if($rowsAffected == 1){
        echo "<strong>$name</strong> has been added..";
      }
    }
}
    
    

  }

  ////////////////////PAGING START ////////////////////////////////
$current_page = isset($_GET['page']) ? $_GET['page'] : 1;
$rpp = 2;//record_per_page

/*-`-`-`-`-`-`-`-`-`-`-`-`-``-`-`-`-`-`-`*/
$totalRecordQuery = "SELECT COUNT(*) as `total` FROM `countries`";
$totalRecordResult  = mysql_query($totalRecordQuery)or die(mysql_error());

$totalRecordAns = mysql_fetch_assoc($totalRecordResult);
/*-`-`-`-`-`-`-`-`-`-`-`-`-``-`-`-`-`-`-`*/
$totalRecords = $totalRecordAns['total'];
$totalPages = ceil($totalRecords / $rpp);
$index = ($current_page - 1)*$rpp;

  ////////////////////PAGING END ////////////////////////////////
  $selectCountryQuery = "SELECT * FROM `countries` ORDER BY id DESC LIMIT $index , $rpp ";
  $selectCountryResult = mysql_query($selectCountryQuery) or die(mysql_error());

//var_dump($selectCountryResult);
echo "Total Countries :".$totalRecords;
?>
<form name="add_country" method="post" action="" enctype="multipart/form-data">
  <input type="hidden" name="id" value="<?=$updateCountryId?>">
  <input type="hidden" name="hidden_c_image" value="<?=$c_image?>">
<table border="1" align="center" width="40%">

  <thead>
    <tr>
      <th colspan="2"><?=$table_title?></th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>Country Name :</td>
      <td><input required type="text" name="name"
value="<?=@$updateCountryName?>"></td>
    </tr>
    <tr>
      <td>Country Image :</td>
      <td><input  type="file" name="c_image">
    <?php if(isset($_GET['mode']) && $_GET['mode']=='edit' && isset($_GET['id'])) {?>
    <img alt="Image of Country" title="country Image" src="c_images/<?=$c_image?>" height="90" width="120">
    <?php }?>
      </td>
    </tr>
    <tr>
      <td>Code</td>
      <td>
   
<!-- Captcha Start -->
<img src="captcha/captcha.php" id="captcha" /><br/>
<a href="#" onclick="    document.getElementById('captcha').src='captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>
<input type="text" required name="captcha" id="captcha-form" /><br/>
<!-- Captcha End-->      
      
      
      </td>
    </tr> 


    <tr>
      <td>&nbsp;</td>
      <td><input type="submit" name="submit" value="<?=$btn_name?>"></td>
    </tr>
  </tbody>
</table>
</form>

<table border="1" align="center" width="40%">

  <thead>
    <tr>
      <th colspan="2">Countries </th>
      <th >C Image </th>
      <th >Action </th>
    </tr>
  </thead>

  <tbody>
<?php
while($selectCountryAns = mysql_fetch_assoc($selectCountryResult)){
$id = $selectCountryAns['id'];
 // pr($selectCountryAns);
?>
    <tr>
      <td><?=$id?></td>
      <td><?=$selectCountryAns['name']?></td>
      <td><img alt="Image of Country" title="country Image" src="c_images/<?=$selectCountryAns['c_image']?>" height="90" width="120"></td>
      <td align="center">
<!---- For delete ---->
<a onClick="return confirm('Are you sure?')"
href="add_country.php?id=<?=$id?>&mode=delete">Delete</a>
<!---- For Edit ---->

<a
href="add_country.php?id=<?=$id?>&mode=edit">Edit</a>

</td>
    </tr>
<?php
}//end while
?>
  </tbody>
</table>

<table border="1" align="center" width="40%">  <tr>
<?php for($i=1 ; $i<=$totalPages ; $i++){  ?>
    <td align="center"><a href="add_country.php?page=<?=$i?>"><?=$i?></a></td>
<?php }?>
  </tr>
<table>
